$(function() {
	var placeholder = "记录您真实的使用后心得感受，写下它的优点。";
	var flag = true;
	var writeArea = $("#content");
	var textError = $('.write-content p');
	var btnSubmit = $('.write-content a');

	$("#score a").on("click",function(){
		$(this).prevAll("a").andSelf().addClass("icon-start-cur");
		$(this).nextAll("a").removeClass("icon-start-cur").parent().attr("size",$(this).index());
	})

	writeArea.keyup(function()
	{
		var str = $(this).val();
		str = str.replace(/\s/g,'');
		var len = $(this).val().length;

		var reg = /^\d*$/;
		var reg1 = /^[a-zA-Z]*$/i;
		var reg2 = /^[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\！\,\.\;\:\"\'\?\!\<\>]*$/;

		if(len > 512){
			$(this).val(str.slice(0, 512));
			$.alert(["温馨提示","试用心得长度为20～512","确定"]);
		}
		
		//除掉标点符号，至少10个汉字
		var char_str=str.replace(/[\！|\、|\—|\；|\（|\）|\：|\·|\“|\‘|\”|\’|\【|\】|\，|\《|\。|\》|\？]/g,"");
		var i = 0;
		for(var a = 0;a < char_str.length;a++)
		{
			if (char_str.charCodeAt(a)>255)
			{
				i++;
			}
		}
    
		if(reg.test(str) || reg1.test(str) || reg2.test(str) || i < 10)
		{
			btnSubmit.attr("class","grey");
			$('#error').show();
		}
		else
		{
			btnSubmit.attr("class","submit");
			$('#error').hide();
		}
	});

    writeArea.bind('copy paste selectstart',function(){
		$.alert(["温馨提示","不要偷懒，自己填写哦！","确定"]);
		return false;
	});
    writeArea.bind("focus",function(){
		(flag) ? $(this).empty():"";
		flag = false;
		return false;
	});

	//显示图片名称
	uploader.on("beforeFileQueued", function(file){
		if(file.size<=1048576 * 2){
			$("#fileName").val(file.name);
		}else{
			$.alert('图片大小不能超过2M');
		}
	});
	//上传图片
	$("#upImage").on("click", function(){
		$("#imageArea span:empty").size()==0?$.alert("最多上传四张图片"):uploader.upload()
	});
	//上传成功
	uploader.on("uploadSuccess", function(file, data){
		$("#fileName").val("");
		uploader.makeThumb(file, function(error, src){
			$("#imageArea span:empty").eq(0).html('<img data-id="'+data.id+'" src="'+src+'">').addClass("cur");
		}, 150, 150);
		
	});
	//上传失败
	uploader.on("uploadError", function(){
		$.alert("网络不给力，上传失败");
	});

	//点击删除图片
	$(document).on("click","#imageArea .cur",function(){
		var thisSpan = $(this);
		$.confirm(["温馨提示","是否删除图片","确认","取消"],function(){
			thisSpan.find("img").remove();
			thisSpan.removeClass("cur");
		});
	})

	//提交评论
	$("#commentsSubmit").on("click", function(){
		if($(this).hasClass("grey"))return false;
		var score = $("#score").children(".icon-start-cur").size();
		var img = $("#imageArea span").map(function(){return $(this).children().attr("data-id")}).get().join(",");
		var content = $("#content").val();
		if(score<1||score>5){
			$.alert("请打分");
		}else if($("#contentError").is(":visible")){
			$.alert("请正确填写新的评论");
		}else{
			$.doAjax({
				url: commentsAddUrl,
				type: "post",
				data: {level: score,img: img, content: content, order_id: orderId, goods_id: goodsId},
				success: function(data){
					if(data.flag){
						$.alert(data.msg, function(){
							$.go(orderDetailUrl);
						})
					}else{
						$.alert(data.msg);
					}
				}
			});
		}
	});
})

function countByLastChar(str){
    var textStr = str.charAt(str.length-1),
    	count = 0;
    for(var i = textStr.length-5; i < str.length; i++){
        if(str.charAt(i) == textStr){
            count++;
        }
    }
    return count;
}
